<?php
/**
 * Created by PhpStorm.
 * User: wdt
 * Date: 2016/4/18
 * Time: 10:36
 */

namespace front\modules\app\controllers;


use common\helpers\ReturnHelper;
use common\RestController;
use front\controllers\RestBaseController;
use goods\services\ShopCategoryProductService;
use goods\models\ShopCategory;
use yii\helpers\ArrayHelper;
use goods\services\ProductService;
use goods\services\ShopCategoryService;

class CategoryController extends RestController{


    /**
     * 获取自定义店铺类目信息
     * @return string
     */
    public function actionCategoryList()
    {
        $shop_id = \Yii::$app->request->post('shop_id');
        if (empty($shop_id)) {
            return ReturnHelper::error('未传递shop_id参数');
        }
        $list = [];

        //获取商品类目
        $oneCategory = ShopCategoryProductService::getShopCategory($shop_id);
        foreach ($oneCategory as $k=>$v) {
            $list[$k]['cid'] = $oneCategory[$k]['shop_cid'];
            $list[$k]['name'] = ShopCategory::getCategoryInfo($v['shop_cid']);
            $thumb = \backend\modules\v1\models\ShopCategory::getCategoryPic($v['shop_cid']);
            $list[$k]['img'] = $thumb;
        }

        //默认加载第一个类目的下级类目
        return ReturnHelper::success('返回结果成功',['list' => $list,'shop_id' => $shop_id]);
    }

    /**
     * 根据店铺ID，类目ID加载对应的商品列表
     * @return string
     */
    public function actionProductList()
    {
        $data = \Yii::$app->request->get();
        if (empty($data['shop_id'])) {
            return ReturnHelper::error('未传递shop_id参数');
        }
        if (empty($data['cid'])) {
            return ReturnHelper::error('未传递shop_cid参数');
        }
        if (!isset($data['page'])) {
            return ReturnHelper::error('未传递page参数');
        }
        if (!isset($data['pre-page'])) {
            return ReturnHelper::error('未传递pre-page参数');
        }
        $products = [];

        // 加载类目商品列表
        if (!empty($data['cid'])) {
            $products = ShopCategoryProductService::searchList(['shop_cid' => $data['cid'],'shop_id' => $data['shop_id'],'status' => 1]);
            $products = ArrayHelper::toArray($products);
        }

        $info = [];

        //获取商品表中正方形的列表显示图
        foreach ($products as $k=>$v) {
            $product = ArrayHelper::toArray(ProductService::ProductInfo($v['pid']));
            if ($product['code'] == 1){
                continue;
            }

            $product = $product['result']['productInfo'];
            $info[$k]['pid'] = $v['pid'];
            $info[$k]['name'] = $product['name'];
            $img = \goods\models\Product::getThumbList($v['pid']);
            $info[$k]['thumb'] = $img.'@1e_1c_0o_0l_150h_150w_100q.jpg';
            $info[$k]['price'] = $product['price'];
        }

        //根据shop_cid获取类目名称，显示在列表页面title中
        $name = ShopCategoryService::getCategoryName($data['cid']);
        return ReturnHelper::success('success',['list' => $info,'shop_id' => $data['shop_id'],'shop_cid' => $data['cid'],'name' => $name]);
    }

}